package com.springboot.controller;

import com.springboot.model.MessageInform;
import com.springboot.model.UserLoginForm;
import com.springboot.service.DoctorService;
import com.springboot.service.DoctorUnreadToReadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;

/**
 * 本控制台用于医生在通知列表实现一键已读
 */
@Controller
public class DoctorAllReadController {
    @Autowired
    DoctorService doctorService;

    @Autowired
    DoctorUnreadToReadService doctorUnreadToReadService;

    @CrossOrigin
    @RequestMapping(value = "/setAllReadServlet")
    public @ResponseBody
    String doctorAllRead(@RequestParam("doctorTel") String doctorTel) {
        System.out.println("医生一键已读");
        try {
            UserLoginForm doctor = doctorService.findUserByTel(doctorTel);
            int doctorId = doctor.getUserId();
            ArrayList<MessageInform> messageInforms = doctorService.findMessageById(doctorId);
            for (int i = 0; i < messageInforms.size(); i++) {

                MessageInform messageInform = messageInforms.get(i);
                System.out.println(messageInform.getMessageId());
                messageInform.setMessageState2("已读");
                doctorUnreadToReadService.updateMessageState(messageInform);
            }
            return "True";
        } catch (Exception e) {
            return "False";
        }
    }
}
